package com.zzyl.nursing.service.impl;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.service.WechaService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WechatServiceImpl implements WechaService {

    //获取OpenID
    private static final String URL_OPENID="https://api.weixin.qq.com/sns/jscode2session";

    //获取AccessToken地址
    public static final String URL_ACCESSTOKEN="https://api.weixin.qq.com/cgi-bin/token ";
    //获取手机号地址
    public static final String URL_PHONE="https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=";

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.appSecret}")
    private String appSecret;


    @Override
    public String getOpenid(String code) {
        //1.封装请求参数
        Map<String, Object> datamap = getConfigApi();
        datamap.put("js_code",code);
        datamap.put("grant_type","authorization_code");
        //2.使用Hutool的HttpUtil发送Get请求
        String result = HttpUtil.get(URL_OPENID, datamap);
        //3.获取响应结果,并解析结果
        JSONObject jsonObject = JSONUtil.toBean(result, JSONObject.class);
        //3.1 errcode=0,表示没有错,可以获取openID
        String openid = jsonObject.getString("openid");
        if (ObjectUtil.isEmpty(openid)){
            throw new BaseException("获取OpenID异常!");
    }
        return openid;
    }

    @NotNull
    private Map<String, Object> getConfigApi() {
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("appid",appId);
        datamap.put("secret",appSecret);
        return datamap;
    }

    @Override
    public String getPhone(String detailCode) {
        //1.定义参数
        Map<String,Object> dataMap=new HashMap<>();
        dataMap.put("code",detailCode);
        //2.执行POST请求
        HttpResponse response = HttpUtil.createPost(URL_PHONE + getAccessToken())
                .body(JSONUtil.toJsonStr(dataMap), "application/json;charset=utf-8")
                .execute();
        //3.解析数据,获取手机号
        String result = response.body();
        JSONObject jsonObject=JSONUtil.toBean(result, JSONObject.class);
        Integer errcode = jsonObject.getInteger("errcode");
        if (jsonObject.getInteger("errcode")!=0){
            throw new BaseException("获取手机号失败!");
        }
        Object path = jsonObject.getByPath("phone_info.purePhoneNumber");

        return path.toString() ;
    }

    /**
     * 获取accessToken
     */
    public String getAccessToken(){
        //1.定义参数
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("grant_type","client_credential");
        datamap.put("appid","wx3ddc11f0c02f2edb");
        datamap.put("secret","4fc033664d4fe17dcb669276b30b7794");
        //2.执行GET请求
        String result = HttpUtil.get(URL_ACCESSTOKEN, datamap);
        //3.解析数据
        JSONObject jsonObject=JSONUtil.toBean(result, JSONObject.class);
        String accessToken = jsonObject.getString("access_token");
        if (ObjectUtil.isEmpty(accessToken)) {
            throw new BaseException("获取AccessToken失败!");
        }
        return accessToken;
    }
}
